﻿--[[
   @brief    group talk for social group
   @author   vincentlao
   @version  1.0
   @date     2015-06-10
--]]

talk_dialog_view = 
{
    pDialog_ = nil;
    dwRootId_ = 0;
    shadow_ptr_ = nil;
--    shadow_id_ = 0;
}

function talk_dialog_view:init()
    base_utility.log("[talk_dialog_view] init ");
end

function talk_dialog_view:show(bShow)
    if self:create() == true and self.pDialog_ ~= nil then
        if bShow == true then
            self.pDialog_:Show(TRUE);
            local web_ptr = ui_utility.get_ctrl_ptr_by_path(self.dwRootId_, "QBWebBrowser_Talk");
            if web_ptr ~= nil and web_ptr:IsVisible() == FALSE then
                web_ptr:Show(TRUE);
            end
            --self.pDialog_:SetFocus(); 有bug http://tapd.oa.com/10041181/bugtrace/bugs/view?bug_id=1110041181053118510&url_cache_key=7e0938d1552398b64df10de6bcb29322
            if not talk_dialog_control.ignore_restore then
                self.pDialog_:Restore();
            end
            self.pDialog_:BringToTop();
        else
            self.pDialog_:Show(FALSE);
        end
    end
end

function talk_dialog_view:create()
    if self.pDialog_ ~= nil then
        return true;
    end
    -- 先创建阴影，防止收不到on_show的通知
--    self.shadow_id_ = layout_mgr.restore_layout_multi("wegame_social\\ChatDlgShadow.tpf");
--    self.shadow_ptr_ = ui_utility.get_ctrl_ptr_by_id(self.shadow_id_);
--    ui_utility.setproperty_by_id(self.shadow_id_, "Width", "714");
--    ui_utility.setproperty_by_id(self.shadow_id_, "Height", "599");

    local social =
        base_utility.get_component_adapter("ISocial_network",
            "ierd_tgp.social_network.Get_social_network_adapter();");
    local webkit_switch = social:get_webkit_switch();
    if webkit_switch == 1 then
        self.dwRootId_ = layout_mgr.restore_layout("social\\social_talk.tpf");
    else
        self.dwRootId_ = layout_mgr.restore_layout("social\\social_talk_ie.tpf");
    end

    if self.dwRootId_ ~= 0 then
        --shadow_helper.create_shadow(self.dwRootId_);
        self.pDialog_ =ui_utility.get_ctrl_ptr_by_id(self.dwRootId_);
        if self.pDialog_ ~= nil then
            self.pDialog_:MoveToCenter();
            self:flush_web();
        end
        return true;
    else
        base_utility.log("[talk_dialog_view] talk_dialog_view error!!!");
        return false;
    end
end

function talk_dialog_view:hide(uilib, ctrl_id)
    base_utility.log("[talk_dialog_view] hide talk_dialog_view tpf");
    if self.pDialog_ ~= nil then
        --shadow_helper.destroy_shadow(self.dwRootId_);
        self.pDialog_:Show(FALSE);
    end
end

function talk_dialog_view:flush_web()
    local web_ctrl = ui_utility.get_ctrl_ptr_by_cid(CID_WEB_CTRL_FOR_TALK_GROUP);
    if web_ctrl ~= nil then        
        local dst_url = web_utility.make_ptlogin_url("/sociality/chat.html", "https");
        local ui_str, str_holder = U8toCUIStr(dst_url);
        web_ctrl:Navigate(ui_str);
    end
end

function talk_dialog_view:close()
    base_utility.log("[talk_dialog_view] remove talk_dialog_view tpf");
    if self.pDialog_ ~= nil then
        self.pDialog_:Show(FALSE);
    end
    --shadow_helper.destroy_shadow(self.dwRootId_);
    layout_mgr.delay_remove_ctrl(self.dwRootId_);
    self.dwRootId_ = 0;
    self.pDialog_ = nil;   
    if self.shadow_ptr_ ~= nil then
        self.shadow_ptr_:Show(FALSE);
    end
--    layout_mgr.delay_remove_ctrl(self.shadow_id_);
--    self.shadow_id_ = 0;
    self.shadow_ptr_ = nil; 
end

function talk_dialog_view:min()
    if self.pDialog_ ~= nil then
        self.pDialog_:Minimize();
    end
end

function talk_dialog_view:has_creat()
    return self.dwRootId_ ~= 0;
end


function talk_dialog_view:resize(width, height)
    base_utility.log("[talk_dialog_view] resize");
  
    if width > 0 and height > 0 then
        if nil ~= self.pDialog_ then
            local rtTemp = self.pDialog_:GetRectEx(FALSE,TRUE);

            rtTemp.right  = rtTemp.left + width + 2;
            rtTemp.bottom = rtTemp.top  + height + 2;

            self.pDialog_:MoveEx(rtTemp,TRUE);
        end;
    end
end

function talk_dialog_view:flash_window()
    if self.pDialog_ ~= nil then
        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        local hwnd = self.pDialog_:GetHwnd();
        social_network:flash_window(hwnd);
    end
end

function talk_dialog_view:is_same_window(hwnd_in)
    local is_same = 0;

    if self.pDialog_ ~= nil then
        local hwnd = self.pDialog_:GetHwnd();
        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        local is_same_window_result = ierd_tgp.social_network.tagIsSameWindow:new();
        social_network:is_same_window(hwnd, hwnd_in, is_same_window_result);
        is_same = is_same_window_result.is_same;
        is_same_window_result:delete();
    end

    return is_same;
end

function talk_dialog_view:get_window()
    if self.pDialog_ ~= nil then
        return self.pDialog_:GetHwnd();
    end

    return 0;
end

function talk_dialog_view:on_show(uilib, ctrl_id)
    base_utility.log("[talk_dialog_view] on_show talk_dialog_view tpf");
    local web_ctrl = ui_utility.get_ctrl_ptr_by_cid(CID_WEB_CTRL_FOR_TALK_GROUP);
    if web_ctrl ~= nil and self.pDialog_:IsDisable() == FALSE then
        web_ctrl:SetFocus();
    end
    if nil ~= self.shadow_ptr_ then
        self.shadow_ptr_:Show(TRUE);
    end
end

--function talk_dialog_view:on_wnd_pos_changed()
--    if nil ~= self.shadow_ptr_ and nil ~= self.pDialog_ then
--        local     rtThis = self.pDialog_:GetRectEx(TRUE,TRUE);
--        local     rtTemp = self.shadow_ptr_:GetRectEx(TRUE,TRUE);
--        local     nWidth = rtThis.right - rtThis.left + 32;
--        local     nHeight= rtThis.bottom - rtThis.top + 32;

--        rtTemp.left  = rtThis.left - 16;
--        rtTemp.top   = rtThis.top  - 6;
--        rtTemp.right = rtTemp.left + nWidth;
--        rtTemp.bottom= rtTemp.top  + nHeight;

--        self.shadow_ptr_:MoveEx(rtTemp,TRUE);
--    end;
--end

function talk_dialog_view:on_hide()
    if nil ~= self.shadow_ptr_ then
        self.shadow_ptr_:Show(FALSE);
    end
end

function talk_dialog_view:set_focus_dialog()
    base_utility.log("[talk_dialog_view] set_focus_dialog talk_dialog_view tpf");
    if self.pDialog_ ~= nil then
        self.pDialog_:SetFocus();
    end
end

function talk_dialog_view:set_close_background(is_single_session)
    if is_single_session then
        local close_btn_id = ui_utility.get_ctrl_id_by_path(self.dwRootId_, "Btn_close");
        ui_utility.setproperty_by_id(close_btn_id, "Background", "ONEIMAGE:client_mainframe\\top_title\\Close_Normal.png;Stretch;0");
        ui_utility.setproperty_by_id(close_btn_id, "BackgroundHover", "ONEIMAGE:client_mainframe\\top_title\\Close_Hover.png;Stretch;0");
        ui_utility.setproperty_by_id(close_btn_id, "BackgroundPressed", "ONEIMAGE:client_mainframe\\top_title\\Close_Press.png;Stretch;0");
    else
        local close_btn_id = ui_utility.get_ctrl_id_by_path(self.dwRootId_, "Btn_close");
        ui_utility.setproperty_by_id(close_btn_id, "Background", "ONEIMAGE:client_social\\close_btn_normal.png;Stretch;0;");
        ui_utility.setproperty_by_id(close_btn_id, "BackgroundHover", "ONEIMAGE:client_social\\close_btn_hover.png;Stretch;0;");
        ui_utility.setproperty_by_id(close_btn_id, "BackgroundPressed", "ONEIMAGE:client_social\\close_btn_normal.png;Stretch;0;");
    end
end

talk_dialog_view.init();

